package javax.mail.search;

public abstract class StringTerm extends SearchTerm {
    protected boolean ignoreCase = true;
    protected String pattern;

    protected StringTerm(String pattern) {
        this.pattern = pattern;
    }

    public String getPattern() {
        return this.pattern;
    }

    protected boolean match(String s) {
        int len = s.length() - this.pattern.length();
        for (int i = 0; i <= len; i++) {
            if (s.regionMatches(this.ignoreCase, i, this.pattern, 0, this.pattern.length())) {
                return true;
            }
        }
        return false;
    }

    public boolean equals(Object obj) {
        if (!(obj instanceof StringTerm)) {
            return false;
        }
        StringTerm st = (StringTerm) obj;
        if (this.ignoreCase) {
            if (st.pattern.equalsIgnoreCase(this.pattern) && st.ignoreCase == this.ignoreCase) {
                return true;
            }
            return false;
        } else if (st.pattern.equals(this.pattern) && st.ignoreCase == this.ignoreCase) {
            return true;
        } else {
            return false;
        }
    }

    public int hashCode() {
        return this.ignoreCase ? this.pattern.hashCode() : this.pattern.hashCode() ^ -1;
    }
}
